//
//  DefaultPreferences.m
//  UIElementInspector
//
//  Copyright 2012 Google Inc. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

#import "Preferences.h"

NSString *preferenceFontSize = @"FontSize";
NSString *preferenceLockInteraction = @"LockInteraction";
NSString *preferenceLockNotifications = @"LockNotifications";
NSString *preferenceNotifications = @"Notifications";
NSString *preferenceNotificationsForSingleElement = @"NotificationsForSingleElement";

@implementation Preferences

// Singleton stuff
static Preferences *sharedPreferences = nil;

+ (Preferences*)sharedPreferences
{
    if (sharedPreferences == nil) {
        sharedPreferences = [[super allocWithZone:NULL] init];
    }
    return sharedPreferences;
}

+ (id)allocWithZone:(NSZone *)zone
{
    return [[self sharedPreferences] retain];
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;
}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return NSUIntegerMax;
}

- (oneway void)release
{
    //do nothing
}

- (id)autorelease
{
    return self;
}
// End singleton stuff (simple, eh?)

+ (NSDictionary*)defaultNotificationEnabled {
    return [NSDictionary dictionaryWithObjectsAndKeys:
            @"1", (NSString*) kAXRowCountChangedNotification,
            @"1", (NSString*) kAXSelectedColumnsChangedNotification,
            @"1", (NSString*) kAXSelectedRowsChangedNotification,
            @"1", (NSString*) kAXDrawerCreatedNotification,
            @"1", (NSString*) kAXHelpTagCreatedNotification,
            @"1", (NSString*) kAXSheetCreatedNotification,
            @"1", (NSString*) kAXCreatedNotification,
            @"1", (NSString*) kAXMovedNotification,
            @"1", (NSString*) kAXResizedNotification,
            @"1", (NSString*) kAXSelectedChildrenChangedNotification,
            @"1", (NSString*) kAXSelectedTextChangedNotification,
            @"1", (NSString*) kAXFocusedUIElementChangedNotification,
            @"1", (NSString*) kAXFocusedWindowChangedNotification,
            @"1", (NSString*) kAXMainWindowChangedNotification,
            @"1", (NSString*) kAXApplicationActivatedNotification,
            @"1", (NSString*) kAXApplicationDeactivatedNotification,
            @"1", (NSString*) kAXApplicationHiddenNotification,
            @"1", (NSString*) kAXApplicationShownNotification,
            @"1", (NSString*) kAXUIElementDestroyedNotification,
            @"1", (NSString*) kAXValueChangedNotification,
            @"1", (NSString*) kAXWindowCreatedNotification,
            @"1", (NSString*) kAXWindowDeminiaturizedNotification,
            @"1", (NSString*) kAXWindowMiniaturizedNotification,
            @"1", (NSString*) kAXWindowMovedNotification,
            @"1", (NSString*) kAXWindowResizedNotification,
            @"1", (NSString*) kAXMenuClosedNotification,
            @"1", (NSString*) kAXMenuItemSelectedNotification,
            @"1", (NSString*) kAXMenuOpenedNotification,
            nil];
}

+ (void)initDefaultPreferences
{
    NSArray *keys = [NSArray arrayWithObjects:
                     preferenceFontSize,
                     preferenceLockInteraction,
                     preferenceLockNotifications,
                     preferenceNotifications,
                     preferenceNotificationsForSingleElement,
                     nil];
    NSArray *values = [NSArray arrayWithObjects:
                       @"9", // FontSize
                       @"1", // LockInteraction,
                       @"1", // LockNotifications,
                       [self defaultNotificationEnabled],
                       @"0", // WholeApplication
                       nil];
    NSDictionary *defaultPreferences = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
    [userPreferences registerDefaults:defaultPreferences];
}

- (NSArray*) allNotifications {
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    return [[(NSDictionary*) notificationEnabled allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

- (BOOL) notificationExists:(NSString*)notification {
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    return [(NSDictionary*) notificationEnabled objectForKey:notification] != nil;
}

- (BOOL) isSystemNotification:(NSString*)notification {
    return [[[self class] defaultNotificationEnabled] objectForKey:notification] != nil;
}

- (void) addNotification:(NSString*)notification {
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    NSMutableDictionary* newNotificationEnabled = [NSMutableDictionary dictionaryWithDictionary:notificationEnabled];
    [newNotificationEnabled setObject:@"1" forKey:notification];
    [[NSUserDefaults standardUserDefaults] setValue:newNotificationEnabled forKey:preferenceNotifications];
}

- (void) removeNotification:(NSString*)notification {
    if ([self isSystemNotification:notification])
        return;
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    NSMutableDictionary* newNotificationEnabled = [NSMutableDictionary dictionaryWithDictionary:notificationEnabled];
    [newNotificationEnabled removeObjectForKey:notification];
    [[NSUserDefaults standardUserDefaults] setValue:newNotificationEnabled forKey:preferenceNotifications];
}

- (NSInteger) isNotificationEnabled: (NSString*) notification {
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    id enabledValue = [(NSDictionary*) notificationEnabled valueForKey:notification];
    return [(NSString*) enabledValue integerValue];
}

- (void) setNotification: (NSString*) notification enabled: (NSInteger) enabled {
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    NSString *stringValue = [NSString stringWithFormat:@"%d", enabled];
    NSMutableDictionary* newNotificationEnabled = [NSMutableDictionary dictionaryWithDictionary:notificationEnabled];
    [newNotificationEnabled setObject:stringValue forKey:notification];
    [[NSUserDefaults standardUserDefaults] setValue:newNotificationEnabled forKey:preferenceNotifications];
}

- (void) setAllNotificationsEnabled: (NSInteger) enabled {
    NSString *stringValue = [NSString stringWithFormat:@"%d", enabled];
    NSMutableDictionary* newNotificationEnabled = [[NSMutableDictionary alloc] init];
    NSArray* all = [self allNotifications];
    for (id notification in [all objectEnumerator])
        [newNotificationEnabled setObject:stringValue forKey:notification];
    [[NSUserDefaults standardUserDefaults] setValue:newNotificationEnabled forKey:preferenceNotifications];
}

- (NSArray*) enabledNotifications {
    id notificationEnabled = [[NSUserDefaults standardUserDefaults] valueForKey:preferenceNotifications];
    return [(NSDictionary*) notificationEnabled allKeysForObject:@"1"];
}

- (NSString*) fontSize
{
    return [[NSUserDefaults standardUserDefaults] stringForKey:preferenceFontSize];
}

- (void) setFontSize:(NSString *)fontSize
{
    [[NSUserDefaults standardUserDefaults] setValue:fontSize forKey:preferenceFontSize];
}

- (NSInteger) lockInteraction
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:preferenceLockInteraction];
}

- (void) setLockInteraction:(NSInteger)lockInteraction
{
    [[NSUserDefaults standardUserDefaults] setInteger:lockInteraction forKey:preferenceLockInteraction];
}

- (NSInteger) lockNotifications
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:preferenceLockNotifications];
}

- (void) setLockNotifications:(NSInteger)lockNotifications
{
    [[NSUserDefaults standardUserDefaults] setInteger:lockNotifications forKey:preferenceLockNotifications];
}

- (BOOL) notificationsForSingleElement
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:preferenceNotificationsForSingleElement];
}

- (void) setNotificationsForSingleElement:(BOOL)notificationsForSingleElement
{
    [[NSUserDefaults standardUserDefaults] setInteger:notificationsForSingleElement forKey:preferenceNotificationsForSingleElement];
}

@end
